package com.lan.iot;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.lan.iot.utils.JsonUtil;
import com.lan.iot.utils.OkHttpRequestUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;
import okhttp3.ResponseBody;

import static com.lan.iot.RecyAdapter.token;

public class AlarmService_2 extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    private BroadcastReceiver alarmReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //定时任务
            //todo
            Log.i("HistoryDataService", "onReceive: 执行保存历史数据");

            //如果版本高于4.4，需要重新设置闹钟

        }
    };
    private void controlDevice(String deviceId, String apiTag, String data) {
        System.out.println("6");
        //发出网络请求
        String SENSOR_DATA_ALL = "http://api.nlecloud.com/Cmds";
        try {
            Map<String, String> valmap = new HashMap<>();
            valmap.put("deviceId", deviceId);
            valmap.put("apiTag", apiTag);
            Response access = OkHttpRequestUtil.postAccess(SENSOR_DATA_ALL, valmap, data, token);
            if (access.isSuccessful()) {
                ResponseBody body = access.body();
                Map map = JsonUtil.toMap(body.string());
                Double status = (double) map.get("Status");
                Message message = new Message();
                if (status == null || status.intValue() != 0.0) {
                    message.arg1 = 0;
                } else {
                    message.arg1 = 1;
                }
                message.obj = map.get("Msg");
                message.what = 0x01;
                handler.sendMessage(message);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0x01:
                    int status = msg.arg1;
                    String msgText = "";
                    if (status == 0) {
                        msgText = "控制失败:" + msg.obj;
                    } else if (status == 1) {
                        msgText = "控制成功:" + msg.obj;
                    }
                    Toast.makeText(AlarmService_2.this, msgText, Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("56789");
        new Thread(new Runnable() {
            @Override
            public void run() {
                controlDevice("638233","timeTask","1");
                //airconditionControl();
            }
        }).start();
//        IntentFilter intentFilter = new IntentFilter("com.alarm.notice");
//        registerReceiver(alarmReceiver, intentFilter);
    }
}
